//
//  MyViewController.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/9.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "MyViewController.h"
#import "MyInformationViewController.h"
#import "SettingViewController.h"
#import "CertificationViewController.h"
#import "MySupplyofGoodsViewController.h"
#import "MyPurchasingViewController.h"
#import "MySaveViewController.h"
#import "MyOrderViewController.h"
#import "RootViewController.h"
#import "RegisteredViewController.h"
#import "LoginViewController.h"
#import "MyHeaderView.h"
#import "MyInformation.h"
#import "SUNSlideSwitchDemoViewController.h"
#import "ExampleViewController.h"
@interface MyViewController ()

@end

@implementation MyViewController{
    int state;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.view setBackgroundColor:UIColorFromRGB(0xf3f2f2)];
    state = 0;
//    [self drawView];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[appStatus shareAppStatus].tabBarController showTabbar];
    [self.navigationController setNavigationBarHidden:YES];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"0"] || [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] == nil) {
        [self initView];
    }else{
        if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"newpassword"]isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"userPwd"]]) {
            [self initView];
        }else{
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                // 耗时的操作
                TIME
                HEADDIC(nowTime, PROFILE);
                NSMutableDictionary *body = [NSMutableDictionary dictionary];
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                [dic setObject:HEAD forKey:@"head"];
                [dic setObject:body forKey:@"body"];
                NSString *result = [Unity requestSomeApiData:HEAD withBody:body];
                NSLog(@"result___%@",result);
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSMutableDictionary *json = [result objectFromJSONString];
                    if ([json objectForKey:@"data"]==nil || [[json objectForKey:@"data"] count]==0) {
                        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"certificate"];
                        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"isLogin"];
                        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"nickname"];
                        [appStatus shareAppStatus].loginSuccess = @"0";
                        [self initView];
                        return;
                    }
                    // 更新界面
                    MyInformation *information = [[MyInformation alloc]init];
                    [information parseResponseData:result];
                    memberModel *member = [information.data objectForKey:@"member"];
                    companyModel *company = [information.data objectForKey:@"company"];
                    if (![Unity isBlankString:company]) {
                        [[NSUserDefaults standardUserDefaults] setObject:company.license_status forKey:@"license_status"];
                    }else{
                        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"license_status"];
                    }
                    [self drawView];
                });
            });
        }
    }
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [[appStatus shareAppStatus].tabBarController hiddenTabbar];
}
-(void)drawView{
    //认证资料，我的货源，我的采购，我的收藏，设置，联络客服
    UIView *tempView1 = (UIView *)[self.view viewWithTag:22];
    if (tempView1) {
        [tempView1 removeFromSuperview];
        tempView1 = nil;
    }
    UIView *view = [self createViewFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT) bgColor:UIColorFromRGB(0xf3f2f2) tag:22];
    [self.view addSubview:view];
    UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 168)];
    tempView.backgroundColor = UIColorFromRGB(0x2578c4);
    [view addSubview:tempView];
    
    UILabel *telLab = [self createLabelFrame:CGRectMake(0, 32, SWIDTH, 12) title:[[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"] titleColor:[UIColor whiteColor] tag:90];
    telLab.font = [UIFont systemFontOfSize:12];
    telLab.textAlignment = NSTextAlignmentCenter;
    [view addSubview:telLab];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(informationClick)];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(informationClick)];
    UIImageView *headImg = [self createImageViewFrame:CGRectMake((SWIDTH - 49)/2, 53, 49, 49) image:nil imageName:@"默认头像" tag:1];
    headImg.userInteractionEnabled = YES;
    [headImg addGestureRecognizer:tap];
    [view addSubview:headImg];
    
    MyHeaderView *headView = [[MyHeaderView alloc]initWithFrame:CGRectMake((SWIDTH - 54)/2, 102, 54, 26)];
    [headView setBackgroundColor:[UIColor clearColor]];
    headView.userInteractionEnabled = YES;
    [headView addGestureRecognizer:tap1];
    [view addSubview:headView];
    
    UIButton *informationBtn = [self createButtonFrame:CGRectMake((SWIDTH-79)/2, headImg.frame.size.height+headImg.frame.origin.y + 27, 79, 21) title:@"我的资料" viewController:self selector:@selector(informationClick) tag:0];
    informationBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [informationBtn setBackgroundImage:[UIImage imageNamed:@"我的资料按钮.png"] forState:0];
    [informationBtn setBackgroundColor:[UIColor clearColor]];
    [view addSubview:informationBtn];
    
    UIButton *orderBtn = [self creatClickBtn:CGRectMake(0, 178, SWIDTH, 43) Title:@"我的订单" Img:@"我的订单" Tag:10];
    [view addSubview:orderBtn];
    
    UIButton *ResourcesBtn = [self creatClickBtn:CGRectMake(0, 231, SWIDTH, 43) Title:@"认证资料" Img:@"认证" Tag:11];
    [view addSubview:ResourcesBtn];
    int license_status = [[[NSUserDefaults standardUserDefaults] objectForKey:@"license_status"] intValue];
    NSString *license_statusStr = @"";
    UIColor *color = nil;
    if (license_status == 0) {
//        license_statusStr = @"未认证";
//        color = GREENColor;
        license_statusStr = @" ";
        color = ClearColor;
    }else if (license_status == 1){
        license_statusStr = @"审核中";
        color = GREENColor;
    }else if (license_status == 2){
        [headView.companyImg setImage:[UIImage imageNamed:@"实名认证"]];
        license_statusStr = @"已认证";
        color = GREENColor;
    }else if (license_status == -1){
        license_statusStr = @"未通过";
        color = REDColor;
    }
    int statusWidth = [license_statusStr sizeWithFont:[UIFont systemFontOfSize:12]
                           constrainedToSize:CGSizeMake(150, 30)
                               lineBreakMode:NSLineBreakByClipping].width;
    UILabel *license_statusLab = [self createLabelFrame:CGRectMake(SWIDTH - statusWidth -25, 31/2, statusWidth, 12) title:license_statusStr titleColor:color tag:0];
    license_statusLab.font = [UIFont systemFontOfSize:12];
    [ResourcesBtn addSubview:license_statusLab];
    
    UIButton *ResourcesBtn1 = [self creatClickBtn:CGRectMake(0, 284, SWIDTH, 43) Title:@"我的货源" Img:@"个人中心货源" Tag:12];
    [view addSubview:ResourcesBtn1];
    
    UIButton *ResourcesBtn2 = [self creatClickBtn:CGRectMake(0, 327, SWIDTH, 43) Title:@"我的采购" Img:@"个人中心采购" Tag:13];
    [view addSubview:ResourcesBtn2];
    
    UIView *lineView1 = [self createViewFrame:CGRectMake(42, 327, SWIDTH - 42, 0.5) bgColor:[UIColor grayColor] tag:0];
    lineView1.alpha = 0.3;
    [view addSubview:lineView1];

    
    UIButton *ResourcesBtn3 = [self creatClickBtn:CGRectMake(0, 380, SWIDTH, 43) Title:@"我的收藏" Img:@"我的收藏" Tag:14];
    [view addSubview:ResourcesBtn3];
    
    UIButton *ResourcesBtn4 = [self creatClickBtn:CGRectMake(0, 423, SWIDTH, 43) Title:@"设置" Img:@"设置" Tag:15];
    [view addSubview:ResourcesBtn4];
    
    UIView *lineView2 = [self createViewFrame:CGRectMake(42, 423, SWIDTH - 42, 0.5) bgColor:[UIColor grayColor] tag:0];
    lineView2.alpha = 0.3;
    [view addSubview:lineView2];
    
    UIButton *callBtn = [self createButtonFrame:CGRectMake(0, 476, SWIDTH, 43) title:@"联络客服：010-53519988" viewController:self selector:@selector(callBtnClick:) tag:0];
    [callBtn setBackgroundColor:[UIColor whiteColor]];
    [callBtn setTitleColor:UIColorFromRGB(0x2578c4) forState:UIControlStateNormal];
    [view addSubview:callBtn];
    
    UIImageView *image = (UIImageView *)[callBtn viewWithTag:909];
    [image setHidden:YES];
    
}
-(void)headImgClick:(UITapGestureRecognizer *)tap{
    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:1];
    if ([tap.view isEqual:imageView]) {
        SHOW_ALERT(@"上传头像");
    }
}
-(void)informationClick{
    [self.navigationController setNavigationBarHidden:NO];
    MyInformationViewController *informationVC = [[MyInformationViewController alloc]init];
    [self.navigationController pushViewController:informationVC animated:YES];
}
-(UIButton *)creatClickBtn:(CGRect)frame
               Title:(NSString *)title
                 Img:(NSString *)img
                 Tag:(NSInteger)tag{
    
    UIButton *button = [self createButtonFrame:frame title:nil viewController:self selector:@selector(myClick:) tag:tag];
    [button setBackgroundColor:[UIColor whiteColor]];
    UILabel *lab = [self createLabelFrame:CGRectMake(42, 0, SWIDTH - 42, 43) title:title titleColor:UIColorFromRGB(0x333333) tag:0];
    [lab setBackgroundColor:[UIColor clearColor]];
    [button addSubview:lab];
    UIImageView *image = [self createImageViewFrame:CGRectMake(10, 11, 21, 21) image:nil imageName:img tag:0];
    [image setBackgroundColor:[UIColor clearColor]];
    [button addSubview:image];
    return button;
}
-(void)myClick:(UIButton *)sender{
    [self.navigationController setNavigationBarHidden:NO];
    //TODO:个人中心页面跳转
    [[appStatus shareAppStatus].tabBarController hiddenTabbar];
    if (sender.tag == 10) {
        MyOrderViewController *orderVC = [[MyOrderViewController alloc]init];
        [self.navigationController pushViewController:orderVC animated:YES];
//        SUNSlideSwitchDemoViewController *demoVC = [[SUNSlideSwitchDemoViewController alloc]init];
//        [self.navigationController pushViewController:demoVC animated:YES];
    }
    if (sender.tag == 11) {
        NSLog(@"认证资料");
        CertificationViewController *cerVC = [[CertificationViewController alloc]init];
        [self.navigationController pushViewController:cerVC animated:YES];
    }else if (sender.tag == 12){
        NSLog(@"货源");
        MySupplyofGoodsViewController *supplyVC = [[MySupplyofGoodsViewController alloc]init];
        supplyVC.title = @"我的货源";
        supplyVC.viewUrl = MYSUPPLY;
        [self.navigationController pushViewController:supplyVC animated:YES];
    }else if (sender.tag == 13){
        NSLog(@"采购");
//        MyPurchasingViewController *purchasingVC = [[MyPurchasingViewController alloc]init];
//        [self.navigationController pushViewController:purchasingVC animated:YES];
        MySupplyofGoodsViewController *supplyVC = [[MySupplyofGoodsViewController alloc]init];
        supplyVC.title = @"我的采购";
        supplyVC.viewUrl = MYPURCHAES;
        [self.navigationController pushViewController:supplyVC animated:YES];
    }else if (sender.tag == 14) {
        MySaveViewController *saveVC = [[MySaveViewController alloc]init];
        [self.navigationController pushViewController:saveVC animated:YES];
    }else if (sender.tag == 15) {
        SettingViewController *setVC = [[SettingViewController alloc]init];
        [self.navigationController pushViewController:setVC animated:YES];
    }
}
-(void)callBtnClick:(UIButton *)sender{
    //TODO: 联络客服
    NSString *num = [[NSString alloc] initWithFormat:@"tel://010-53519988"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]];
}
//创建登录界面
-(void)initView{
    UIView *tempView = (UIView *)[self.view viewWithTag:21];
    if (tempView) {
        [tempView removeFromSuperview];
        tempView = nil;
    }
    UIView *view = [self createViewFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT) bgColor:[UIColor clearColor] tag:21];
    [self.view addSubview:view];
    UIImageView *bgImg = [self createImageViewFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT) image:nil imageName:@"图层-4" tag:0];
    [view addSubview:bgImg];
    
    UIImageView *smallImg = [self createImageViewFrame:CGRectMake((SWIDTH - 80)/2, 140,  80, 80) image:nil imageName:@"启动" tag:0];
    smallImg.backgroundColor = [UIColor clearColor];
    [view addSubview:smallImg];
    
    UILabel *titleLab = [self createLabelFrame:CGRectMake(0, smallImg.frame.size.height+smallImg.frame.origin.y +20, SWIDTH, 25) title:@"更受欢迎的板材交易中心" titleColor:[UIColor whiteColor] tag:0];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [view addSubview:titleLab];
    
    UILabel *versionLab = [self createLabelFrame:CGRectMake(0, titleLab.frame.size.height + titleLab.frame.origin.y +10, SWIDTH, 10) title:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] titleColor:BLACKColor tag:0];
    versionLab.textAlignment = NSTextAlignmentCenter;
    versionLab.font =[UIFont systemFontOfSize:10];
    [self.view addSubview:versionLab];
    
    UIButton *tempBtn1 = [self createButtonFrame:CGRectMake(20, SHEIGHT - 179 , SWIDTH - 40, 43) title:@"注册有材网" viewController:self selector:@selector(registeredClick:) tag:10];
    [tempBtn1 setBackgroundImage:[UIImage imageNamed:@"注册登录默认按钮"] forState:UIControlStateNormal];
    tempBtn1.titleLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:tempBtn1];
    
    UIButton *tempBtn2 = [self createButtonFrame:CGRectMake(20, SHEIGHT - 122, SWIDTH - 40, 43) title:@"登录有材网" viewController:self selector:@selector(loginClick:) tag:10];
//    [tempBtn2 setBackgroundImage:[UIImage imageNamed:@"注册登录默认按钮"] forState:UIControlStateNormal];
    [tempBtn2 setBackgroundColor:[UIColor whiteColor]];
    [tempBtn2 setTitleColor:[UIColor blackColor] forState:0];
    tempBtn2.titleLabel.font = [UIFont systemFontOfSize:14];
    tempBtn2.layer.masksToBounds = YES;
    tempBtn2.layer.cornerRadius = 20.0;
    tempBtn2.layer.borderWidth = 0.6;
    tempBtn2.layer.borderColor = [[UIColor whiteColor] CGColor];
    [view addSubview:tempBtn2];
    
}
-(void)registeredClick:(UIButton *)sender{
    //TODO:注册
    RegisteredViewController *registerVC = [[RegisteredViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}
-(void)loginClick:(UIButton *)sender{
    //TODO:登录
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
}
//创建UILabel  tag值为三位数
- (UILabel *)createLabelFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)color
                          tag:(NSInteger)tag{
    UILabel *tempLabel = [[UILabel alloc]initWithFrame:frame];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setText:title];
    [tempLabel setTag:tag];
    [tempLabel setTextColor:color];
    return tempLabel;
}
//创建UIButton tag值为四位数
- (UIButton *)createButtonFrame:(CGRect)frame
                          title:(NSString *)title
                 viewController:(id)viewController
                       selector:(SEL)selector
                            tag:(NSInteger)tag{
    UIButton *tempButton = [UIButton buttonWithType:0];
    [tempButton setFrame:frame];
    [tempButton setTag:tag];
    [tempButton setBackgroundColor:[UIColor clearColor]];
    [tempButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tempButton setTitle:title forState:UIControlStateNormal];
    [tempButton addTarget:viewController action:selector forControlEvents:UIControlEventTouchUpInside];
    UIImageView *imageV = [self createImageViewFrame:CGRectMake(SWIDTH - 20, (43 - 12)/2, 7, 12) image:nil imageName:@"引导@2x" tag:909];
    [tempButton addSubview:imageV];
    return tempButton;
}
//创建UIImageView tag值为五位数
- (UIImageView *)createImageViewFrame:(CGRect)frame
                                image:(UIImage *)image
                            imageName:(NSString *)imageName
                                  tag:(NSInteger)tag{
    UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:frame];
    [tempImageView setBackgroundColor:[UIColor clearColor]];
    if (image) {
        tempImageView.image = image;
    }else{
        tempImageView.image = [UIImage imageNamed:imageName];
    }
    [tempImageView setTag:tag];
    return tempImageView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
